package com.nghiatran.networks;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import com.nghiatran.utils.Utils;
import android.util.Log;
 /**
 * @author TRAN NGHIA 
 * @Email  tranducnghiabkhn@gmail.com
 */

public class HttpHelper {

	public HttpHelper() {
		// TODO Auto-generated constructor stub
	}
    private static final String TAG = "HttpHelper";

    public static String getData(String url) {
        StringBuilder builder = new StringBuilder();
        try {
            HttpClient client = HttpUtils.getNewHttpClient();
            HttpGet request = new HttpGet(url);

            HttpResponse httpResponse = client.execute(request);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();

            BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
            String line = "";
            while ((line = buffer.readLine()) != null) {
                builder.append(line);
            }

        } catch (Exception e) {
            Log.e(TAG, "Failed to download data: " + e.toString());
            return null;
        }
        return builder.toString();
    }

    public static String getData(String url, List<NameValuePair> nameValuePairs) {
        String result = null;
        HttpClient client = HttpUtils.getNewHttpClient();
        HttpPost request = new HttpPost(url);
        try {
            request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(request);
            result = Utils.inputStreamToString(response.getEntity().getContent());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static String getData(String url, List<NameValuePair> nameValuePairs, String photoPath) {
        String result = null;
        HttpClient client = HttpUtils.getNewHttpClient();
        HttpPost request = new HttpPost(url);
        try {
            request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            FileEntity photo = new FileEntity(new File(photoPath), "jpg");
            request.setEntity(photo);

            HttpResponse response = client.execute(request);
            result = Utils.inputStreamToString(response.getEntity().getContent());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }


}
